<?php
$app_config = require(dirname(__FILE__) . '/../../config/boot.php');
require $app_config['QEEPHP_DIR'] . '/library/q.php';
require $app_config['APP_DIR'] . '/myapp.php';
$ret = MyApp::instance($app_config);

if (isset($_REQUEST['key']) && ($_REQUEST['key'] == Q::ini('custom_system/queue_key')))
{
	$listing = Online_Ebay_Listing::find('online_ebay_listing.schedule_flag=?', Q::ini('custom_flag/online_ebay_listing_schedule_flag/set/value'))->order('schedule_next_time asc')
		->asArray()->getOne();
	if ($listing && strtotime($listing['schedule_next_time']) <= CURRENT_TIMESTAMP)
	{ 
		$result = Online_Ebay_Listing::pushAdd($listing['id'], Q::ini('custom_flag/ebay_listing_push_type/schedule/value'));
		$listing_in_db = Online_Ebay_Listing::find('id=?', $listing['id'])->getOne();
		if ($result['ack'] == SUCCESS)
		{
			if ($listing['schedule_repeat'] == Q::ini('custom_flag/online_ebay_listing_schedule_repeat/repeat/value'))
			{
				$listing_in_db->schedule_next_time = date('Y-m-d H:i:s', time() + 3600 * 24 * $listing['schedule_repeat_step']);
			}
			else
			{
				$listing_in_db->schedule_flag = Q::ini('custom_flag/online_ebay_listing_schedule_flag/not_set/value');
			}
		}
		else
		{
			$listing_in_db->schedule_flag = Q::ini('custom_flag/online_ebay_listing_schedule_flag/wrong/value');
		}
		$listing_in_db->save();
	}
}
